
/********************************************************/
/*                    Bui Phuc Dat                      */
/*             Hiển thị nhãn và tooltip                 */
/********************************************************/
var _dialog;
var url = document.getElementById("txtRequest").value
		+ "/Config/alias.xml";
var urlFont = document.getElementById("txtRequest").value
+ "/Config/config-font.xml";
var objRead = null;
var arrField = [];
var arrAlias = [];
var font_Style_Name=[];
var font_Style_Alias=[];
var font_Family_Name=[];
var font_Family_Alias=[];
var font_Color_Name=[];
var font_Color_Alias=[];
var arr_checkbox_id=[];
var bLoad = false;
var _select=null;
var _ctrlSelectFeatures=null;
var check_id;
var _value_check;
var _item=0;
function cbo_checked_change(element){
	if(element.checked){
		arr_checkbox_id[_item]=element.id;
		_item++;
	}else{
		arr_checkbox_id =new Array();
	}
}

function item_checking(array,value){
	for(var i=0;i<array.length;i++){
		if(array[i]==value)
			return true;
	}
	return false;
}
// get layer selected
function getLayerID() {
	var _index = document.getElementById("select_Tool_cboLayers").selectedIndex;
	var _text = document.getElementById("select_Tool_cboLayers").options[_index].text;
	return _text;
}
// set label for vector
function getSelect(chk,family,color,style,size) {
	var strlabel = '';
	for ( var i = 0; i < chk.length; i++) {
		if (chk[i].checked) {
			strlabel += chk[i].title + ": " + '$' + "{" + chk[i].value + "}"
					+ "\n";
		}
	}
	var _fontFamily=_getElementById(family);
	var _fontColor=_getElementById(color);
	var _fontStyle=_getElementById(style);
	var _fontSize=_getElementById(size);
	var vectorStyle = new OpenLayers.Style();
	var rule = new OpenLayers.Rule({
		symbolizer : {
		     strokeWidth: 2,
		     label:strlabel,
		     fontColor: _fontColor[_fontColor.selectedIndex].value,
		     fontFamily: _fontFamily[_fontFamily.selectedIndex].value,
		     fontWeight:_fontStyle[_fontStyle.selectedIndex].value,
		     fontStyle: _fontStyle[_fontStyle.selectedIndex].value,
		     fontSize: _fontSize[_fontSize.selectedIndex].value,
		}
	});
	vectorStyle.addRules([ rule ]);
	var _value = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].value;
	var vector = map.layers[_value];
	vector.styleMap=new OpenLayers.StyleMap(vectorStyle);
	vector.refresh({force:true});
	_dialog.hide();
	//_dialog.destroy().remove();
}
// open dialog attribute
function selectAttribute(url) {
	var text = getLayerID();
	AUI().use('aui-dialog', 'aui-io', 'event', 'event-custom', function(A) {
		_dialog = new A.Dialog({
			title : "Lớp " + text,
			centered : true,
			draggable : true,
			modal : true,
			width : 230,
			height : 'auto',
		}).plug(A.Plugin.IO, {
			uri : url
		}).render();
		_dialog.show();
		loadArrAttribute(text);
	});	
}
// read attribute feature from xml file
function loadxml(layerName) {
	var i = 0, j = 0;
	if(layerName=='Hố ga'){
		layerName='DWA_HoGa';
	}else if(layerName=='Điểm đấu nối'){
		layerName='DWA_DiemDauNoi';
	}
	$.get(url, function(data) {
		$(data).find('alias').each(function() {
			var $field = $(this);
			$field.find('Field').each(function() {
				var param = $(this);
				var _layerNames = param.attr('layerName');
				if (layerName == _layerNames) {
					var name = param.attr('Name');
					var alias = param.attr('Alias');
					arrField[i++] = name;
					arrAlias[j++] = alias;
				}
			});
			bLoad = true;
		});
	});
	return arrField;
}

// read font family from xml file 
function load_Font_Family(){
	var i = 0, j = 0;
	$.get(urlFont, function(data) {
		$(data).find('font').each(function() {
			var $field = $(this);
			$field.find('FontFamily').each(function() {
				var family = $(this);
				var fname = family.attr('Name');
				var falias = family.attr('Alias');
				font_Family_Name[i++]=fname;
				font_Family_Alias[j++]=falias;
			});
		});
	});
}

// read font style from xml file
function load_Font_Style(){
	var i = 0, j = 0;
	$.get(urlFont, function(data) {
		$(data).find('font').each(function() {
			var $field = $(this);
			$field.find('FontStyle').each(function() {
				var style = $(this);
				var sname = style.attr('Name');
				var salias = style.attr('Alias');
				font_Style_Name[i++]=sname;
				font_Style_Alias[j++]=salias;
			});
		});
	});
}

// read font color from xml file
function load_Font_Color(){
	var i = 0, j = 0;
	$.get(urlFont, function(data) {
		$(data).find('font').each(function() {
			var $field = $(this);
			$field.find('FontColor').each(function() {
				var style = $(this);
				var sname = style.attr('Name');
				var salias = style.attr('Alias');
				font_Color_Name[i++]=sname;
				font_Color_Alias[j++]=salias;
			});
		});
	});
}
function read() {
	if (!bLoad) {
		objRead = setTimeout("read();", 1000);
	} else {
		clearTimeout(objRead);
	}
}

function loadArrAttribute(layer) {
	bLoad = false;
	arrField = new Array();
	arrAlias = new Array();
	loadxml(layer);
	load_Font_Family();
	load_Font_Style();
	load_Font_Color();
	read();
}
// set tooltip feature
function Set_Tooltip_Feature(vectorObj){
	var ctrlSelectFeatures = new OpenLayers.Control.SelectFeature(vectorObj, {
		hover : true,
		onSelect: onSelectFeature,
		onUnselect: onUnSelectFeature
	});
	_ctrlSelectFeatures=ctrlSelectFeatures;
	map.addControl(ctrlSelectFeatures);
	ctrlSelectFeatures.activate();
}
// function mouse over feature
function onSelectFeature(feature){
	_select=feature;
	var _selected_text = _select_Tool_cboLayers[_select_Tool_cboLayers.selectedIndex].text;
	var strShow="";
	if(_selected_text=="Hố ga"){
		strShow="Mã hố ga: "+feature.attributes["MaHoGa"];
	}else if(_selected_text=="Điểm đấu nối"){
		strShow="Mã điểm đấu nối: "+feature.attributes["MaDiemDN"];
	}
	var _pop = new OpenLayers.Popup.FramedCloud("HoverPop",
			feature.geometry.getBounds().getCenterLonLat(), null,
			"<div style='font-size:.8em;'>" +strShow  + "</div>",
			null, false, onPopupClose);
	_pop.contentDiv.style.padding='3px';
	_pop.contentDiv.style.margin='0';
	_pop.autoSize = true;
	feature.popup = _pop;
	map.addPopup(_pop);
}
// function mouse out feature
function onUnSelectFeature(feature){
	map.removePopup(feature.popup);
	feature.popup.destroy();
}

function onPopupClose(evt){
	_ctrlSelectFeatures.unselect(_select);
}
/********************************************************/
/*                        The end                       */
/********************************************************/